//
//  IABrowseResultCell.m
//  VoiceClass
//
//  Created by caoxiaobo on 2018/1/13.
//  Copyright © 2018年 YRX. All rights reserved.
//

#import "IABrowseResultCell.h"
#import "UIView+Extension.h"
#import "IASubjectItem.h"
#import "IAStudent.h"
#import "Definition.h"

static CGFloat resultFont15 = 15.0f;
static CGFloat resultFont17 = 17.0f;

@implementation IABrowseResultItem

- (void)setResult:(IASubjectAnswerResult *)result {
    _result = result;
    IASubjectItem *subject = result.subjectItem;
    
    CGFloat lrMarigin = 6.0f;
    CGFloat outMargin = 6.0;
    CGFloat udMarigin = 12.0f;
    CGFloat cellWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat containerWidth = cellWidth - lrMarigin * 2;
    
    CGFloat normalLabelH = 22.0f;
    CGFloat lrPadding = 8.0f;
    CGFloat validContainerW = containerWidth - lrPadding * 2;
    
    _questionTypeLabelFrame = CGRectMake(lrPadding, outMargin, validContainerW, normalLabelH);
    CGFloat questionLabelH = [subject.question
                              boundingRectWithSize:CGSizeMake(validContainerW, CGFLOAT_MAX)
                              options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:resultFont17]}
                              context:nil].size.height;
    _questionLabelFrame = CGRectMake(lrPadding,
                                     CGRectGetMaxY(_questionTypeLabelFrame) + udMarigin,
                                     validContainerW,
                                     questionLabelH);
    _answerDescLabelFrame = CGRectMake(lrPadding,
                                       CGRectGetMaxY(_questionLabelFrame) + udMarigin,
                                       validContainerW,
                                       normalLabelH);
    CGFloat answerLabelH = [subject.answer
                            boundingRectWithSize:CGSizeMake(validContainerW, CGFLOAT_MAX)
                            options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:resultFont17]}
                            context:nil].size.height;
    _answerLabelFrame = CGRectMake(lrPadding,
                                   CGRectGetMaxY(_answerDescLabelFrame) + udMarigin,
                                   validContainerW,
                                   answerLabelH);
    _middleLineViewFrame = CGRectMake(0,
                                      CGRectGetMaxY(_answerLabelFrame) + udMarigin,
                                      containerWidth,
                                      0.5);
    
    CGFloat avatarWH = 60.0f;
    CGFloat avatarRPadding = 12.0f;
    CGFloat avatarX = validContainerW - avatarWH - avatarRPadding;
    CGFloat avatarY = CGRectGetMaxY(_middleLineViewFrame) + udMarigin + udMarigin * 0.5;
    _avatarFrame = CGRectMake(avatarX, avatarY, avatarWH, avatarWH);
    
    CGFloat nameLabelW = containerWidth - avatarRPadding * 3 - avatarWH;
    _nameLabelFrame = CGRectMake(avatarRPadding,
                                 CGRectGetMinY(_avatarFrame),
                                 nameLabelW,
                                 normalLabelH);
    _gradeLabelFrame = CGRectMake(avatarRPadding,
                                  CGRectGetMaxY(_nameLabelFrame) + udMarigin,
                                  nameLabelW,
                                  normalLabelH);
    
    _bottomLineViewFrame = CGRectMake(0,
                                      CGRectGetMaxY(_gradeLabelFrame) + udMarigin,
                                      containerWidth,
                                      0.5);
    
    _goalLabelFrame = CGRectMake(avatarX,
                                 CGRectGetMinY(_bottomLineViewFrame) + udMarigin,
                                 avatarWH,
                                 avatarWH);
    
    CGFloat studentAnswerW = CGRectGetMinX(_goalLabelFrame) - avatarRPadding * 2;
    CGFloat studentAnswerH = [result.studentAnswer
                              boundingRectWithSize:CGSizeMake(studentAnswerW, CGFLOAT_MAX)
                              options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:resultFont17]}
                              context:nil].size.height;
    _studentAnswerLabelFrame = CGRectMake(avatarRPadding,
                                          CGRectGetMinY(_goalLabelFrame) + 6.0f,
                                          studentAnswerW,
                                          studentAnswerH);
    
    CGFloat maxHeight = MAX(CGRectGetMaxY(_goalLabelFrame) + avatarRPadding,
                            CGRectGetMaxY(_studentAnswerLabelFrame) + avatarRPadding);
    _containerViewFrame = CGRectMake(lrMarigin, outMargin, containerWidth, maxHeight + 6.0);
    
    _cellHeight = _containerViewFrame.size.height + 2 * outMargin;
}

@end

@interface IABrowseResultCell ()

@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) UILabel *questionLabel;
@property (nonatomic, strong) UILabel *questionTypeLabel;
@property (nonatomic, strong) UILabel *answerDescLabel;
@property (nonatomic, strong) UILabel *answerLabel;

@property (nonatomic, strong) UIView *middleLineView;

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *gradeLabel;

@property (nonatomic, strong) UIView *bottomLineView;

@property (nonatomic, strong) UILabel *studentAnswerLabel;
@property (nonatomic, strong) UILabel *goalLabel;

@end

@implementation IABrowseResultCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor whiteColor];
        
        _questionTypeLabel = [self assembleLabel:resultFont15
                                       textColor:[UIColor darkGrayColor]
                                        numLines:1];
        _questionLabel = [self assembleLabel:resultFont17
                                   textColor:HEXCOLOR(0x000030)
                                    numLines:0];
        _answerDescLabel = [self assembleLabel:resultFont15
                                     textColor:[UIColor darkGrayColor]
                                      numLines:1];
        _answerLabel = [self assembleLabel:resultFont17
                                 textColor:HEXCOLOR(0x000030)
                                  numLines:0];
        _middleLineView = [[UIView alloc] init];
        _middleLineView.backgroundColor = [UIColor lightGrayColor];
        
        _avatarView = [[UIImageView alloc] init];
        _avatarView.layer.cornerRadius = 23.0f;
        _avatarView.clipsToBounds = YES;
        
        _nameLabel = [self assembleLabel:resultFont17
                               textColor:HEXCOLOR(0x000030)
                                numLines:1];
        _gradeLabel = [self assembleLabel:resultFont17
                                textColor:HEXCOLOR(0x000030)
                                 numLines:1];
        
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = [UIColor lightGrayColor];
        
        _studentAnswerLabel = [self assembleLabel:resultFont17
                                        textColor:HEXCOLOR(0x000030)
                                         numLines:0];
        _goalLabel = [self assembleLabel:resultFont17 textColor:[UIColor redColor] numLines:1];
        _goalLabel.font = [UIFont boldSystemFontOfSize:19];
        _goalLabel.backgroundColor = HEXCOLOR(0xf6f6f6);
        _goalLabel.layer.cornerRadius = 23.0f;
        _goalLabel.clipsToBounds = YES;
        _goalLabel.textAlignment = NSTextAlignmentCenter;
        
        [_containerView addSubview:_questionTypeLabel];
        [_containerView addSubview:_questionLabel];
        [_containerView addSubview:_answerDescLabel];
        [_containerView addSubview:_answerLabel];
        
        [_containerView addSubview:_middleLineView];
        
        [_containerView addSubview:_goalLabel];
        [_containerView addSubview:_avatarView];
        [_containerView addSubview:_nameLabel];
        
        [_containerView addSubview:_bottomLineView];
        
        [_containerView addSubview:_gradeLabel];
        [_containerView addSubview:_studentAnswerLabel];
        self.contentView.backgroundColor = CMNormalBg;
        [self.contentView addSubview:_containerView];
    }
    return self;
}

- (UILabel *)assembleLabel:(CGFloat)fontSize textColor:(UIColor *)tColor numLines:(NSInteger)numLines {
    UILabel *aLabel = [UILabel new];
    aLabel.textColor = tColor;
    aLabel.numberOfLines = numLines;
    aLabel.font = [UIFont systemFontOfSize:fontSize];
    return aLabel;
}

- (void)setItem:(IABrowseResultItem *)item {
    _item = item;
    
    IASubjectAnswerResult *result = item.result;
    
    self.questionTypeLabel.text = [NSString stringWithFormat:@"%@问题", result.subjectItem.subjectName];
    self.questionLabel.text = result.subjectItem.question;
    self.answerDescLabel.text = @"标准答案";
    self.answerLabel.text = result.subjectItem.answer;
    
    self.nameLabel.text = result.student.name;
    self.gradeLabel.text = [NSString stringWithFormat:@"%@班", result.student.grade];
    self.avatarView.image = [UIImage imageNamed:result.student.avatar];
    self.studentAnswerLabel.text = result.studentAnswer;
    self.goalLabel.text = result.goal;
    
    self.containerView.frame = item.containerViewFrame;
    self.questionTypeLabel.frame = item.questionTypeLabelFrame;
    self.questionLabel.frame = item.questionLabelFrame;
    self.answerDescLabel.frame = item.answerDescLabelFrame;
    self.answerLabel.frame = item.answerLabelFrame;
    self.middleLineView.frame = item.middleLineViewFrame;
    
    self.nameLabel.frame = item.nameLabelFrame;
    self.gradeLabel.frame = item.gradeLabelFrame;
    self.avatarView.frame = item.avatarFrame;
    self.bottomLineView.frame = item.bottomLineViewFrame;
    
    self.studentAnswerLabel.frame = item.studentAnswerLabelFrame;
    self.goalLabel.frame = item.goalLabelFrame;
    
}

@end

